package net.xiaosaguo.mymall.order.controller;

import net.xiaosaguo.mymall.order.entity.OrderEntity;
import net.xiaosaguo.mymall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * description: rabbit 测试
 *
 * @author xiaosaguo
 * @date 2020/07/09
 */
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMessage() {
        // System.out.println(Thread.currentThread().getId());
        CompletableFuture.runAsync(() -> {
            // System.out.println(Thread.currentThread().getId());
            for (int i = 0; i < 10; i++) {
                if (i % 2 == 0) {
                    OrderReturnReasonEntity reason = new OrderReturnReasonEntity();
                    reason.setId(1L);
                    reason.setCreateTime(new Date());
                    reason.setName("哈哈" + i);
                    reason.setSort(100);

                    // 如果发送的消息是个对象，会使用序列化机制，将对象写出去，所以对象需要实现序列化接口
                    rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", reason,
                            new CorrelationData(UUID.randomUUID().toString()));
                    // System.out.println("1-rabbitTemplate：{}，发送成功：" + reason);
                } else {
                    OrderEntity order = new OrderEntity();
                    order.setOrderSn(UUID.randomUUID().toString());
                    // rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", order,
                    rabbitTemplate.convertAndSend("hello-java-exchange", "xxx.hello.java", order,
                            new CorrelationData(UUID.randomUUID().toString()));
                    // System.out.println("2-rabbitTemplate：{}，发送成功：" + order);
                }
            }
        });
        return "ok";
    }
}
